import { defineStore } from 'pinia';
import { getVideoUrl, previewURLs } from '@/api/video';
import { getRightData } from '@/api/earlyWarning';
import { onMounted, ref } from 'vue';
import COMPONENT_TITLES from '@/constants/components/componentsTitle';
import { adjustWebSocketUrl } from '@/utils/adjustWebSocketUrl';

interface VideoParams {
  cameraIndexCode: string;
  protocol: string;
}

interface VideoDevice {
  code: string;
  type?: string;

  [key: string]: any;

  // 其他可能的属性
}

export const useVideoPlayerStore = defineStore('videoPlayer', () => {
  // 状态
  const videoValue = ref('');
  const highPressureRePlayUrls = ref('');
  const videoType = ref('');

  const xlVideoDevices = ref<VideoDevice[]>([]);
  const nyVideoDevices = ref<VideoDevice[]>([]);
  const currentRealIndex = ref(0);
  const currentNyIndex = ref(0);
  const realPlayUrl = ref('');
  const nyPlayUrl = ref('');
  const xlVideoType = ref('');
  const reValue = ref('');
  const highValue = ref('');
  const realParams = ref<VideoParams>({
    cameraIndexCode: '',
    protocol: 'ws'
  });
  const nyParams = ref<VideoParams>({
    cameraIndexCode: '',
    protocol: 'ws'
  });
  const realCameraType = ref(''); // 实时监控的分类用于保存摄像头类型 控制是否展示云台操作
  const nyCameraType = ref(''); // 第三方的摄像头分类

  // 获取视频播放URL
  const fetchVideoUrl = async (params: VideoParams) => {
    try {
      const { data } = await previewURLs(params);
      return adjustWebSocketUrl(data);
    } catch (error) {
      console.error('Failed to fetch video URL:', error);
    }
  };

  // 获取特殊视频URL
  const fetchSpecialVideoUrl = async (serialNumber: string) => {
    try {
      const res = await getVideoUrl({ serialNumber });
      highPressureRePlayUrls.value = res.data;
    } catch (error) {
      console.error('Failed to fetch special video URL:', error);
      highPressureRePlayUrls.value = '';
    }
  };

  // 初始化视频设备
  const initializeVideoDevices = async () => {
    try {
      const res = await getRightData({ type: 'video' });
      xlVideoDevices.value = res.data.xlVideo || [];
      nyVideoDevices.value = res.data.nyVideo || [];
      reValue.value = xlVideoDevices.value[0].name;
      highValue.value = nyVideoDevices.value[0].name;
      // 初始化线路视频
      if (xlVideoDevices.value.length > 0) {
        await getRealPlayUrlHandle(xlVideoDevices.value[0]);
      }

      // 初始化高压视频
      if (nyVideoDevices.value.length > 0) {
        await extractionNyVideoHandle(nyVideoDevices.value[0]);
      }
    } catch (error) {
      console.error('Failed to initialize video devices:', error);
    }
  };
  //实时监控获取播放code逻辑
  const getRealPlayUrlHandle = async (device: any) => {
    realParams.value.cameraIndexCode = device.code;
    realCameraType.value = device.cameraType;
    xlVideoType.value = device.type;
    realPlayUrl.value = await fetchVideoUrl(realParams.value);
  };
  //抽出第三方播放的逻辑
  const extractionNyVideoHandle = async (device: any) => {
    const firstDevice = device;
    videoType.value = firstDevice.type || '';
    nyCameraType.value = firstDevice.cameraType || '';
    if (videoType.value === '0') {
      nyParams.value.cameraIndexCode = firstDevice.code;
      nyPlayUrl.value = await fetchVideoUrl(nyParams.value);
    } else if (videoType.value === '1') {
      await fetchSpecialVideoUrl(firstDevice.code);
    } else if (videoType.value === '3') {
      highPressureRePlayUrls.value = firstDevice.code;
    }
  };
  // 点击事件处理
  const handleDeviceClick = async (index: string, name: string) => {
    console.log('Device clicked:', { index, name });
    // 这里可以添加实际的点击处理逻辑
    if (name === COMPONENT_TITLES.REAL_TIME_MONITORING) {
      const item = xlVideoDevices.value.find((item) => item.code === index);
      await getRealPlayUrlHandle(item);
    } else if (name === COMPONENT_TITLES.THIRD_PARTY_DISRUPTION_REAL_TIME_MONITORING) {
      const item = nyVideoDevices.value.find((item) => item.code === index);
      await extractionNyVideoHandle(item);
    }
  };

  // 生命周期钩子
  onMounted(async () => {
    await initializeVideoDevices();
  });

  return {
    videoValue,
    realPlayUrl,
    nyPlayUrl,
    xlVideoType,
    videoType,
    highPressureRePlayUrls,
    xlVideoDevices,
    nyVideoDevices,
    currentRealIndex,
    currentNyIndex,
    realParams,
    nyParams,
    realCameraType,
    nyCameraType,
    highValue,
    reValue,
    fetchVideoUrl,
    fetchSpecialVideoUrl,
    handleDeviceClick
  };
});
